import QtQuick 1.1
import com.nokia.meego 1.0

import "DucankoStyle.js" as DucankoStyle

Page {
    // EditItemsPage
    id: editItemsPage

    property variant itemDatabaseDialog
    property variant messageBox
    property variant selectDialog

    property int groupIndex : 0
    property string groupButtonText

    //sectionCenter
    Item {
        id: sectionCenter

        anchors {
            horizontalCenter: parent.horizontalCenter
            top: sectionTop.bottom
        }

        height: parent.height-sectionBottom.height-sectionTop.height
        width: parent.width

        ListView {
            id: list

            anchors.fill: parent
            width: parent.width

            highlight: Rectangle {
                color: "lightsteelblue"
                radius: 5
                width: list.width
            }
            highlightMoveSpeed: -1
            model: modelItems
            currentIndex: -1
            delegate: Rectangle {
                height: isAGroup?50:70
                width: parent.width

                property int groupnum: groupNumber

                color: isAGroup?(groupnum%5===1?"#FF931E":groupnum%5===2?"#3EA8F4":groupnum%5===3?"#79C842":
                                 groupnum%5===4?"#FE1C24":"#FE7AAB"):"transparent"
                Image {
                    id: lvImage
                    height: parent.height - 20
                    width: isAGroup?0:height
                    source: isAGroup?"":"qrc:/images/prazno3.png" //stavil sivo

                    fillMode: Image.PreserveAspectFit
                    anchors {
                        left: parent.left
                        verticalCenter: parent.verticalCenter
                        leftMargin: 10
                        rightMargin: 10
                    }
                }
                Text {
                    id: txtLeft
                    anchors {
                        left: lvImage.right
                        verticalCenter: parent.verticalCenter
                        leftMargin: 10
                    }
                    text: isAGroup?groupName:name
                    font.family: DucankoStyle.FONT_FAMILY
                    font.pixelSize: isAGroup?DucankoStyle.FONT_LIST_GROUP_SIZE:DucankoStyle.FONT_LIST_ITEM_SIZE
                    font.bold: isAGroup


                }
                MouseArea {
                    id: mArea
                    anchors.fill: parent
                    onClicked: {
                        list.currentIndex = isAGroup?-1:index
                    }

                    onDoubleClicked: {
                        list.currentIndex = isAGroup?-1:index

                        if (list.currentIndex > 0)
                            buttonEdit.buttonEditClicked()
                    }

                    onPressed: {
                        list.currentIndex = isAGroup?-1:index
                    }

                    onPressAndHold: {
                        list.currentIndex = isAGroup?-1:index
                        if (list.currentIndex > 0)
                            buttonEdit.buttonEditClicked()
                    }
                }
            }
        }
    }

    //sectionTop
    Item {
        id: sectionTop

        anchors {
            horizontalCenter: parent.horizontalCenter

            top: parent.top
            left: parent.left

            //margins: 10
        }

        height: buttonGrupa.height
        width: parent.width

        Image{
            id: bgTop
            anchors.fill: parent
            source: "qrc:/images/diopozadine-01gore.png"
            fillMode: "Stretch"
        }

        Button {
            id: buttonGrupa
            text: groupButtonText //qsTr("All groups");
            anchors{
                top: parent.top
                left: parent.left
                horizontalCenter: parent.horizontalCenter
            }

            width: parent.width;

            onClicked: openDialog()

            ToolIcon {
                id: icon
                anchors {
                    right: parent.right
                    verticalCenter: parent.verticalCenter
                }
                iconId: "icon-m-toolbar-down"
                onClicked: parent.openDialog()

            }

            style: ButtonStyle{ horizontalAlignment: Text.AlignLeft }

            function openDialog(){
                var component = Qt.createComponent("MySelectionDialog.qml")
                selectDialog = component.createObject(editItemsPage, {"model":modelGroups})

                selectDialog.titleText = qsTr("Select group")

                //selectDialog.model = modelGroups

                //selectDialog.selectedIndex = groupIndex //dodal

                selectDialog.accepted.connect(accept)
                selectDialog.rejected.connect(reject)

                selectDialog.open()
            }

            function accept(){
                groupButtonText = selectDialog.model.get(selectDialog.selectedIndex).name
                groupIndex = selectDialog.selectedIndex
                qmlHandler.refreshItems(selectDialog.model.get(selectDialog.selectedIndex).groupId,-1)
                list.currentIndex=-1
                selectDialog.destroy();
            }

            function reject(){
                selectDialog.destroy()
            }
        }
    }

    //sectionBottom
    Item {
        id: sectionBottom

        anchors {
            horizontalCenter: parent.horizontalCenter
            left: parent.left
            //top: sectionCenter.bottom
            bottom: parent.bottom
        }

        width: parent.width
        height: sectionButtonsRound.height+sectionButtonsNormal.height+sectionButtonsRound.anchors.margins*3
        //parent.height*0.27

        Image{
            id: bgBottom
            anchors.fill: parent
            source: "qrc:/images/diopozadine-02dolje.png"
            fillMode: "Stretch"
        }

        //sectionButtonsRound
        Item{
            id: sectionButtonsRound

            height: row.itemHeight
            width: parent.width-row.columns*row.itemWidth //ovo ovisi o sirini gumbi

            anchors {
                horizontalCenter: parent.horizontalCenter

                bottom: sectionButtonsNormal.top
                left: parent.left

                margins: 10
            }

            Grid {
                id: row

                anchors.fill: parent
                anchors.horizontalCenter: parent.horizontalCenter

                //anchors.margins: 10

                width: parent.width
                height: parent.height

                rows: 1
                columns: 2

                property int itemWidth: 80//Math.round((width-columns*spacing)/columns)//parent.height-2*spacing; //Math.round((width - spacing * (columns - 1)) / columns)
                property int itemHeight: 80 //itemWidth //Math.round((height - spacing * (rows - 1)) / rows)

                spacing: (width-columns*itemWidth)/(columns-1)

                RoundButton {
                    width: row.itemWidth; height: row.itemHeight;
                    imgSource: "qrc:/images/plus.png";
                    imgPressedSource: "qrc:/images/plus2.png";
                    imgDisabledSource: "qrc:/images/plus3.png";

                    isEnabled: modelGroups.count>1 //gave segmentation fault when trying to add an item when no groups existed yet

                    function buttonClicked(){
                        var component = Qt.createComponent("ItemDatabaseDialog.qml")
                        itemDatabaseDialog = component.createObject(editItemsPage)

                        itemDatabaseDialog.titleString=qsTr("Add Item")
                        itemDatabaseDialog.message=qsTr("Item name: ")
                        itemDatabaseDialog.textFieldText=qsTr("New item")
                        itemDatabaseDialog.selectAll()
                        itemDatabaseDialog.acceptButtonText=qsTr("Add")
                        itemDatabaseDialog.rejectButtonText=qsTr("Cancel")
                        itemDatabaseDialog.groupIndex = (groupIndex == 0) ? groupIndex : groupIndex - 1
                        itemDatabaseDialog.accepted.connect(add)

                        itemDatabaseDialog.open()
                    }

                    function add(name, groupId){
                        if (qmlHandler.editItems_Add(name, groupId, groupIndex)){
                            itemDatabaseDialog.destroy()
                        }else{
                            var component = Qt.createComponent("MessageBox.qml")
                            messageBox = component.createObject(editItemsPage)

                            messageBox.type="Error"
                            messageBox.message=qsTr("An item with this name already exists!")
                            messageBox.open()
                        }
                    }

                }

                RoundButton {
                    width: row.itemWidth; height: row.itemHeight;
                    imgSource: "qrc:/images/minus.png";
                    imgPressedSource: "qrc:/images/minus2.png";
                    imgDisabledSource: "qrc:/images/minus3.png";

                    isEnabled: if (list.currentIndex != -1){
                                   true
                               }else{
                                   false
                               }

                    function buttonClicked(){
                        var component = Qt.createComponent("MessageBox.qml")
                        messageBox = component.createObject(editItemsPage)

                        messageBox.type="Question"
                        messageBox.message=qsTr("Delete item: ") + list.model.get(list.currentIndex).name + "?"
                        messageBox.acceptedClicked.connect(yes)
                        messageBox.open()
                    }

                    function yes(){
                        qmlHandler.editItems_Delete(list.model.get(list.currentIndex).itemId, groupIndex)
                        list.currentIndex = -1;
                        messageBox.destroy()
                    }
                }
            }

        }

        //sectionButtonsNormal
        Item{
            id: sectionButtonsNormal

            width: parent.width
            // height: parent.height-sectionButtonsRound.height
            height: grid.rows*(grid.itemHeight)+grid.spacing

            // color: "yellow"

            anchors {
                horizontalCenter: parent.horizontalCenter
                //top: sectionButtonsRound.bottom
                //bottom: parent.bottom
                left: parent.left
                bottom: parent.bottom
                margins: 10
            }

            Grid {
                id: grid

                anchors.fill: parent
                //anchors.margins: 10

                columns: 2
                rows: 1
                spacing: 6

                width: parent.width
                height: parent.height

                property int itemWidth: Math.round((width - spacing * (columns - 1)) / columns)
                property int itemHeight: 51; //Math.round((height - spacing * (rows - 1)) / rows)

                Button{
                    id: buttonBack;
                    text: qsTr("Back");
                    width: grid.itemWidth;
                    height: grid.itemHeight;
                    onClicked: appWindow.pageStack.pop();
                }

                Button{
                    id: buttonEdit;
                    text: qsTr("Edit");
                    width: grid.itemWidth;
                    height: grid.itemHeight;
                    enabled: list.currentIndex > 0

                    property variant dialog
                    property variant tmp

                    onClicked: {
                        buttonEditClicked()
                    }

                    function buttonEditClicked(){
                        var component = Qt.createComponent("ItemDatabaseDialog.qml")
                        itemDatabaseDialog = component.createObject(editItemsPage)

                        itemDatabaseDialog.titleString=qsTr("Edit Item")
                        itemDatabaseDialog.message=qsTr("Item name: ")
                        itemDatabaseDialog.textFieldText=list.model.get(list.currentIndex).name
                        itemDatabaseDialog.openKeyboard()
                        itemDatabaseDialog.acceptButtonText=qsTr("Save")
                        itemDatabaseDialog.rejectButtonText=qsTr("Cancel")
                        itemDatabaseDialog.groupIndex = list.model.get(list.currentIndex).groupNumber - 1
                        itemDatabaseDialog.accepted.connect(save)

                        itemDatabaseDialog.open()
                    }

                    function save(name, groupId){
                        if (name == list.model.get(list.currentIndex).name){
                            itemDatabaseDialog.destroy()
                        }else if (qmlHandler.editItems_Rename(name, list.model.get(list.currentIndex).itemId)){
                            list.model.get(list.currentIndex).name=name
                            itemDatabaseDialog.destroy()
                        }else{
                            var component = Qt.createComponent("MessageBox.qml")
                            messageBox = component.createObject(editItemsPage)

                            messageBox.type="Error"
                            messageBox.message=qsTr("An item with this name already exists!")
                            messageBox.open()

                            return
                        }

                        if (groupId != list.model.get(list.currentIndex).groupId){
                            qmlHandler.editItems_ChangeGroup(list.model.get(list.currentIndex).itemId, groupId, groupIndex)
                        }
                    }
                }
            }
        }
    }

/*
    MySelectionDialog {
        id: e
        titleText: qsTr("Select group")

        model: modelGroups

        selectedIndex: groupIndex //dodal

        onAccepted: {
            //buttonGrupa.text= dialogGrupa.model.get(dialogGrupa.selectedIndex).name
            groupButtonText= dialogGrupa.model.get(dialogGrupa.selectedIndex).name
            groupIndex = dialogGrupa.selectedIndex
            qmlHandler.refreshItems(dialogGrupa.model.get(dialogGrupa.selectedIndex).groupId)
        }

        onRejected: {
            console.log("rejection")
        }
    }
*/
}
